package initialzation

import (
	"server/model"
	"server/myutils"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

var (
	DB  *gorm.DB
	err error
)

func InitDataBase() {
	DB, err = gorm.Open(mysql.Open(myutils.ConnectionInfo), &gorm.Config{NamingStrategy: schema.NamingStrategy{SingularTable: true}})
	if err != nil {
		panic("数据库连接出现错误")
	}
	synchronizeDatabase()
	model.MoveDB(DB)
}

//数据库同步函数
func synchronizeDatabase() {
	DB.AutoMigrate(
		&model.Category{},
		&model.User{},
		&model.Admin{},
		&model.Article{},
		&model.Comment{},
	)
}
